package components;

import java.security.InvalidParameterException;

import data.RCAlertSMSCommand;
import data.RCDeviceSMSCommand;
import data.RCSMSCommand;
import data.RCUpdateSMSCommand;
import data.RecievingSMSCommand;
import data.SMSCommand;
import data.SMSLog;
import data.SendingSMSCommand;

public class SMSRecievingCommandFactory {
	public static RCSMSCommand createSMSCommandFromString(String phoneNumber, String smsMessage) {
		
		if (smsMessage == null || phoneNumber == null) {
			throw new InvalidParameterException();
		}
		
		String cmdCode =null;
		try {
			cmdCode = getCmdCode(smsMessage);
		} catch (Exception e) {
			throw new InvalidParameterException("invalid message format");
		}
		
		RCSMSCommand smsCommand = null;
		
		//RecievingSMSCommand recievingMessage = new RecievingSMSCommand(phoneNumber, smsMessage);
		if (cmdCode.equalsIgnoreCase(SMSCommand.CMD_CODE_ALARM)) {
			smsCommand = new RCAlertSMSCommand(phoneNumber, smsMessage);
		} else if (cmdCode.equalsIgnoreCase(SMSCommand.CMD_CODE_DEVICE)) {
			smsCommand = new RCDeviceSMSCommand(phoneNumber, smsMessage);
		} else if (cmdCode.equalsIgnoreCase(SMSCommand.CMD_CODE_HOME)) {
			
		} else if (cmdCode.equalsIgnoreCase(SMSCommand.CMD_CODE_POWER)) {
			
		} else if (cmdCode.equalsIgnoreCase(SMSCommand.CMD_CODE_UPDATE)) {
			smsCommand = new RCUpdateSMSCommand(phoneNumber, smsMessage);
		}
		else {
			smsCommand = new RCSMSCommand(phoneNumber, smsMessage);
		}
		
		return smsCommand;
//String[] components = smsMessage.split(" ");
		
//		if (components.length != 3) {
//			throw new InvalidParameterException("");
//		}
//		
//		String cmdCode = components[0];
//		String deviceIndexes = Integer.parseInt(components[1]);
//		mIsTurnOn = components[2].equals(STATE_ON) ? true : false;
	}

	/**
	 * 
	 * @param smsMessage
	 * @exception InvalidParameterException
	 * @return
	 */
	private static String getCmdCode(String smsMessage) {
		// TODO Auto-generated method stub
		
		if (smsMessage == null || smsMessage.length() < 3) {
			throw new InvalidParameterException("invalid message format");
		}
		
			return smsMessage.substring(0, 3);
		
	}
	
//	public static String createSMSCommandString(SendingSMSCommand smsCommand) {
//		return smsCommand.toString();
//	}
}
